package net.huashitong.user.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.util.MD5;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.oversee.filter.SupervisionTaskFilter;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.Department;
import net.huashitong.user.entity.Role;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.filter.DepartmentFilter;
import net.huashitong.user.filter.RoleFilter;
import net.huashitong.user.service.BnpUserService;
import net.huashitong.user.service.DepartmentService;
import net.huashitong.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/deptManageSuper")
public class DeptManageController extends JsonBaseController {
    @Override
    protected String getView(String s) {
        return "/modules/deptManage/" + s;
    }

    @Autowired
    private BnpUserService bnpUserService;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(method = RequestMethod.GET)
    public String initPage(HttpServletRequest request, Model model) {
        model.addAttribute("leftType", "用户管理");
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        if (ObjectUtils.isBlank(bnpUser)) {
            return "/modules/login/login";
        }
        //待办任务个数
        int nodeCount = (int) WebUtils.getSessionAttribute(request, "nodeCount");
        int myTaskNum = (int) WebUtils.getSessionAttribute(request, "myTaskNum");
        int quetionNum = (int) WebUtils.getSessionAttribute(request, "quetionNum");
        model.addAttribute("user", bnpUser);
        model.addAttribute("nodeCount", nodeCount);
        model.addAttribute("myTaskNum", myTaskNum);
        model.addAttribute("quetionNum", quetionNum);
        return getView("query");
    }

    @RequestMapping("/filter")
    public String filter(@ModelAttribute SupervisionTaskFilter filter, Model model) {
        return getView("filter");
    }

    //初始化数据（查询部门信息）
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute DepartmentFilter filter, HttpServletRequest request, Model model) {
        //默认查询业主单位部门信息
        List<Department> list = departmentService.queryByFilter(filter);
        return coverJqGrid(filter, list);
    }


    /**
     * 创建单位
     */
    @RequestMapping(value = "/createDept", method = RequestMethod.GET)
    public String createDept(@ModelAttribute BnpUserFilter filter) {
        return getView("createDept");
    }
    /**
     * 创建单位
     **/
    @ResponseBody
    @RequestMapping(value = "/createDept", method = RequestMethod.POST)
    public Message createDepartmentInfo(@ModelAttribute DepartmentFilter filter) {
        departmentService.addDepartment(filter);
        return coverMessage("200", "单位添加成功");
    }

    /**
     * 修改单位
     */
    @RequestMapping(value = "/editDept", method = RequestMethod.GET)
    public String editDept(@RequestParam String sysId, Model model) {
        Department departMent = departmentService.getById(sysId);
        model.addAttribute("departMent", departMent);
        return getView("editDept");
    }
    /**
     * 修改单位
     **/
    @ResponseBody
    @RequestMapping(value = "/editDept", method = RequestMethod.POST)
    public Message editDeptInfo(@ModelAttribute Department newDept) throws NoSuchFieldException {
        String sysId = "";
        if(newDept.getSysId().contains(",")){
            String [] arr = newDept.getSysId().split(",");
            sysId = arr[0];
            newDept.setSysId(sysId);
        }
        Department oldDept = departmentService.getById(newDept.getSysId());
        departmentService.updateDepartment(oldDept,newDept);
        return coverMessage("200", "单位修改成功");
    }






}
